﻿namespace FiftyOne.Foundation.Mobile.Browser
{
    using System;
    using System.Collections.Generic;
    using System.Drawing.Imaging;

    internal class Support
    {
        private static List<ColorsToBitsPerPixel> _colorTable = null;
        private static Dictionary<string, ImageFormat> _formats = null;
        private static object _lock = new object();

        private static void AddPair(List<ColorsToBitsPerPixel> colorTable, int bitsPerPixel, long colors)
        {
            colorTable.Add(new ColorsToBitsPerPixel(bitsPerPixel, colors));
        }

        internal static int GetBitsPerPixel(long colors)
        {
            foreach (ColorsToBitsPerPixel pixel in ColorTable)
            {
                if (colors <= pixel.Colors)
                {
                    return pixel.BitsPerPixel;
                }
            }
            return 8;
        }

        private static List<ColorsToBitsPerPixel> ColorTable
        {
            get
            {
                if (_colorTable == null)
                {
                    lock (_lock)
                    {
                        _colorTable = new List<ColorsToBitsPerPixel>();
                        AddPair(_colorTable, 1, 2L);
                        AddPair(_colorTable, 2, 4L);
                        AddPair(_colorTable, 3, 8L);
                        AddPair(_colorTable, 4, 0x10L);
                        AddPair(_colorTable, 5, 0x20L);
                        AddPair(_colorTable, 6, 0x40L);
                        AddPair(_colorTable, 7, 0x80L);
                        AddPair(_colorTable, 8, 0x100L);
                        AddPair(_colorTable, 9, 0x200L);
                        AddPair(_colorTable, 10, 0x400L);
                        AddPair(_colorTable, 11, 0x800L);
                        AddPair(_colorTable, 12, 0x1000L);
                        AddPair(_colorTable, 13, 0x2000L);
                        AddPair(_colorTable, 14, 0x4000L);
                        AddPair(_colorTable, 15, 0x8000L);
                        AddPair(_colorTable, 0x10, 0x10000L);
                        AddPair(_colorTable, 0x11, 0x20000L);
                        AddPair(_colorTable, 0x12, 0x40000L);
                        AddPair(_colorTable, 0x13, 0x80000L);
                        AddPair(_colorTable, 20, 0x100000L);
                        AddPair(_colorTable, 0x15, 0x200000L);
                        AddPair(_colorTable, 0x16, 0x400000L);
                        AddPair(_colorTable, 0x17, 0x800000L);
                        AddPair(_colorTable, 0x18, 0x1000000L);
                        AddPair(_colorTable, 0x19, 0x2000000L);
                        AddPair(_colorTable, 0x1a, 0x4000000L);
                        AddPair(_colorTable, 0x1b, 0x8000000L);
                        AddPair(_colorTable, 0x1c, 0x10000000L);
                        AddPair(_colorTable, 0x1d, 0x20000000L);
                        AddPair(_colorTable, 30, 0x40000000L);
                        AddPair(_colorTable, 0x1f, 0x80000000L);
                        AddPair(_colorTable, 0x20, 0x100000000L);
                    }
                }
                return _colorTable;
            }
        }

        internal static Dictionary<string, ImageFormat> Formats
        {
            get
            {
                if (_formats == null)
                {
                    lock (_lock)
                    {
                        _formats = new Dictionary<string, ImageFormat>();
                        _formats.Add("*/*", ImageFormat.Png);
                        _formats.Add("image/png", ImageFormat.Png);
                        _formats.Add("image/gif", ImageFormat.Gif);
                        _formats.Add("image/jpeg", ImageFormat.Jpeg);
                        _formats.Add("image/bmp", ImageFormat.Bmp);
                        _formats.Add("image/tiff", ImageFormat.Tiff);
                    }
                }
                return _formats;
            }
        }
    }
}

